package cs9322.assign3.resources;

import javax.print.attribute.standard.Media;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;

import cs9322.assign3.dao.OrderDao;
import cs9322.assign3.dao.OrdersDao;
import cs9322.assign3.model.Order;


public class OrderResource {
	@Context
	UriInfo uriInfo;
	@Context
	Request request;
	int id;
	public OrderResource(UriInfo uriInfo, Request request, int id) {
		this.uriInfo = uriInfo;
		this.request = request;
		this.id = id;
	}
	
	@OPTIONS
	@Produces(MediaType.TEXT_XML)
	public String getOptions(){
		OrderDao od = new OrderDao();
		Order o = od.findById(id);
		if(o.getStatus().equalsIgnoreCase("waiting")){
			return "GET PUT DELETE";
		}else return"GET";
		
	}
	
	
	@GET
	@Produces(MediaType.TEXT_XML)
	public Order getOrderHTML() {
//		Order o = OrdersDao.instance.getStore().get(Integer.toString(id));
//		if(o == null)
//			throw new RuntimeException("GET: Order with id:" + id +  " not found");
		OrderDao od = new OrderDao();
		Order o = od.findById(id);
		return o;
	}

	@PUT
	@Produces(MediaType.TEXT_XML)
	@Consumes(MediaType.TEXT_XML)
	public Order putOrder(Order o){
		Order order = o;
		double price = priceCalculator(order.getType(), order.getSize());
		o.setPrice(price);
//		putAndGetResponse(order);
		OrderDao od = new OrderDao();
		od.update(o);
		return order;
		
	}

	
	@DELETE
	public void deleteOrder() {
//		Order delo = OrdersDao.instance.getStore().remove(Integer.toString(id));
//		if(delo==null)
//			throw new RuntimeException("DELETE: order with id:" + id +  " not found");
		OrderDao od = new OrderDao();
		od.delete(id);
		
	}
	
	
	private double priceCalculator(String type, String size){
		if(type.equals("mocha")){
			if(size.equals("large")){
				return 3.30;
			}else if(size.equals("medium")){
				return 3.60;
			}else if(size.equals("large")){
				return 4.20;
			}
			}
		else if(type.equals("black long")){
			if(size.equals("large")){
				return 3.20;
			}else if(size.equals("medium")){
				return 3.50;
			}else if(size.equals("large")){
				return 4.00;
		}}
		else if(type.equals("Cappuccino")){
			if(size.equals("large")){
				return 3.10;
			}else if(size.equals("medium")){
				return 3.60;
			}else if(size.equals("large")){
				return 4.10;
			}
		}
		return 0;
	}
	
}
